<?php
Prado::using('Application.common.common');
class Profile extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | My Profile";
		
		if (!$this->IsPostBack)
		{			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::Notice;
				if ($this->Request['m'] == 'uu') {					
					$this->Notice->Text = "<p style='text-align:center;'>Your profile has been updated successfully.</p>";			
				}
				if ($this->Request['m'] == 'up') {					
					$this->Notice->Text = "<p style='text-align:center;'>Your password has been updated successfully.</p>";			
				}
			}
			
			$activeRecord = $this->getItem();
			if ($activeRecord && $activeRecord->ID > 0)
			{
				$this->txtFirstName->Text = $activeRecord->FirstName;
				$this->txtLastName->Text = $activeRecord->LastName;
				$this->txtEmail->Text = $activeRecord->Email;
				$this->txtPhone->Text = $activeRecord->Phone;
				$this->txtPhone2->Text = ($activeRecord->Phone2 != "") ? $activeRecord->Phone2 : "-";
				$this->txtAddress->Text = $activeRecord->Address;
				$this->txtCity->Text = $activeRecord->City;
				$this->txtCountry->Text = Common::getCountryName($activeRecord->Country);
				$this->txtPostalCode->Text = $activeRecord->PostalCode;
				$this->txtCredits->Text = ($activeRecord->Credits == null ? 0 : $activeRecord->Credits);				
				$this->txtCreditsUsed->Text = ($activeRecord->CreditsUsed == null ? 0 : $activeRecord->CreditsUsed);				
				$this->txtCreditsAvailable->Text = ($activeRecord->Credits == null ? 0 : $activeRecord->Credits) - ($activeRecord->CreditsUsed == null ? 0 : $activeRecord->CreditsUsed);
				$criteria = new TActiveRecordCriteria;
				$criteria->Condition = "order_id > 0 and user_id = " . $this->Application->User->ID;						
				$orderRecord = Prado::createComponent("OrderRecord");
				$this->txtNumOrders->Text = count($orderRecord->finder()->findAll($criteria));				
			}
		}
	}	
	
	protected function getItem()
	{
		$activeRecord = UserRecord::finder()->findByuser_idAnduser_email($this->Application->User->ID, $this->Application->User->Email);
		if($activeRecord === null)
		{
			$this->Notice->Type = UserNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_NOT_FOUND","user");
			$this->mainPanel->Visible = false;
		}
		return $activeRecord;
	}
	
	protected function btnContinue_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("Index"));
	}
}

?>